﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
public partial class admin_paypalDeposit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null) { Response.Redirect("../Default.aspx"); }
        //permiso();

        if (!Page.IsPostBack)
        {

            int id = 0;
            ArrayList array = new ArrayList();

           
            if (Request.QueryString["id"] != null) id = int.Parse(Request.QueryString["id"].ToString());
            if (id != 0)
            {// si viene a ver un deposito en especial
                array.Add(id.ToString());
                DataSet dsdep = DB.getDepositsPay(4, array);
                if (dsdep.Tables[0].Rows.Count > 0)
                {
                    tb_iddep.Text = dsdep.Tables[0].Rows[0][0].ToString();
                    tb_reference.Text = dsdep.Tables[0].Rows[0][3].ToString();
                    tb_date.Text = dsdep.Tables[0].Rows[0][4].ToString();
                    //tb_amount.Text = dsdep.Tables[0].Rows[0][5].ToString();
                    
                    lb_total.Visible = true;
                    tb_total.Visible = true;
                }
                DataSet dsorder = DB.GetTranbyDep(id.ToString());
                if (dsorder != null)
                {
                    gv_deposit.DataSource = dsorder;
                    gv_deposit.DataBind();
                    int cont2 = dsorder.Tables[0].Rows.Count - 1;
                    if (cont2 >= 0)
                    {

                        string tst = String.Format("{0:0.00}", double.Parse(dsorder.Tables[0].Rows[cont2][8].ToString()));
                        tb_total.Text = tst;
                    }
                    else { tb_total.Text = "0"; }
                }
                btn_addcheck.Visible = true;
                bt_finish.Visible = true;
                btn_edit.Visible = true;
                enableControls(false);
                bt_save.Visible = false;



            }
            else
            {
                enableControls(true);
                bt_finish.Visible = true;
                btn_addcheck.Visible = false;
                bt_save.Visible = true;
                tb_iddep.Text = "0";
                lb_total.Visible = false;
                tb_total.Visible = false;
            }


        }
        else
        {
            int id = 0;
            if (Request.QueryString["id"] != null) id = int.Parse(Request.QueryString["id"].ToString());
            DataSet dsorder = DB.GetTranbyDep(id.ToString());
            if (dsorder != null)
            {
                int cont2 = dsorder.Tables[0].Rows.Count - 1;
                if (cont2 >= 0)
                {

                    string tst = String.Format("{0:0.00}", double.Parse(dsorder.Tables[0].Rows[cont2][5].ToString()));
                    tb_total.Text = tst;
                }
                else
                {
                    tb_total.Text = "0";
                }
                //DataSet dsdeposits = DB.getDeposits(5, null);
                gv_deposit.DataSource = dsorder;
                gv_deposit.DataBind();
            }

        }
        int comprobar = comprobarsum();
       


    }
    protected void lnk_update_Click(object sender, EventArgs e)
    {
        if (validar())
        {

            RE_GenericBean re = new RE_GenericBean();

            re.strC1 = tb_reference.Text;
            //re.strC3 = tb_amount.Text;
            re.strC2 = tb_date.Text;
            int id = 0;
            if (!tb_iddep.Text.Equals("")) id = int.Parse(tb_iddep.Text);

            int resp = DB.addDepositPaypal(re, id);
            if (resp != 0)
            {
                tb_iddep.Text = resp.ToString();
                btn_addcheck.Visible = true;
                btn_addcheck.Enabled = true;
                bt_save.Visible = false;
                btn_edit.Visible = true;
                lnk_update.Visible = false;


            }
            else
            {

                Response.Write("<script> alert('Deposit not saved ');</script>");
            }

            DataSet dsorder = DB.GetTranbyDep(id.ToString());
            if (dsorder != null)
            {
                int cont2 = dsorder.Tables[0].Rows.Count - 1;
                if (cont2 >= 0)
                {
                    string tst = String.Format("{0:0.00}", double.Parse(dsorder.Tables[0].Rows[cont2][5].ToString()));
                    tb_total.Text = tst;
                }
                else
                {
                    tb_total.Text = "0";
                }
                gv_deposit.DataSource = dsorder;
                gv_deposit.DataBind();
            }
        }
    
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        if (validar())
        {
            RE_GenericBean re = new RE_GenericBean();
            // re.intC1 = int.Parse(cb_bank1.SelectedValue);
            re.strC1 = tb_reference.Text;
           // re.strC3 = tb_amount.Text;
            re.strC2 = tb_date.Text;
            int id = 0;
            if (!tb_iddep.Text.Equals("")) id = int.Parse(tb_iddep.Text);

            int resp = DB.addDepositPaypal(re, id);
            if (resp != 0)
            {
                tb_iddep.Text = resp.ToString();
                btn_addcheck.Visible = true;
                btn_addcheck.Enabled = true;
                bt_save.Visible = false;
                btn_edit.Visible = true;
                Response.Redirect("detailTrans.aspx?dep=" + resp.ToString());

            }
            else
            {

                Response.Write("<script> alert('Deposit not saved ');</script>");
            }

            DataSet dsorder = DB.GetTranbyDep(id.ToString());
            if (dsorder != null)
            {
                int cont2 = dsorder.Tables[0].Rows.Count - 1;
                if (cont2 >= 0)
                {
                    string tst = String.Format("{0:0.00}", double.Parse(dsorder.Tables[0].Rows[cont2][5].ToString()));
                    tb_total.Text = tst;
                }
                else
                {
                    tb_total.Text = "0";
                }
                gv_deposit.DataSource = dsorder;
                gv_deposit.DataBind();
            }
        }
    }

    private bool validar()
    {
        int result = 1;
        if (tb_reference.Text.Equals("")) { result = result * 0; }
      
        if (tb_date.Text.Equals("")) { result = result * 0; }
     
        

        if (result == 1) return true; else return false;
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        enableControls(true);
        btn_edit.Visible = false;
        bt_save.Visible = false;
        btn_addcheck.Visible = false;
        lnk_update.Visible = true;
    }
    protected void bt_finish_Click(object sender, EventArgs e)
    {
        /*int comprobar = comprobarsum();
        if (comprobar == 0) {close_modal.Show(); }
        if (comprobar == 1) { Response.Redirect("Accounting.aspx"); }
        if (comprobar == 2) { close_modal.Show(); }*/
        Response.Redirect("Accounting.aspx"); 
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        int id = 0;
        if (Request.QueryString["id"] != null) id = int.Parse(Request.QueryString["id"].ToString());
        if (id != 0)
        {
            Session["newCheck"] = 1;
            Response.Redirect("detailTrans.aspx?dep=" + id.ToString());
        }
        else
        {

            Response.Write("<script language='javascript'>alert('First select a deposit in order to submit a new Transaction');<script>");
        }
    }

    private void enableControls(bool p)
    {
       // cb_bank1.Enabled = p;
      //  tb_amount.Enabled = p;
        tb_date.Enabled = p;
        tb_reference.Enabled = p;
        bt_save.Enabled = p;


    }

    protected void gv_deposit_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Style.Add("display", "none");//ordernum
           e.Row.Cells[9].Style.Add("display", "none");//SUM
            e.Row.Style.Add("text-align", "left");

        }
        if (e.Row.RowType == DataControlRowType.Header)
        {
          /*  e.Row.Cells[0].Text = "Edit";
            e.Row.Cells[2].Text = "Bank";
            e.Row.Cells[3].Text = "Client";
            e.Row.Cells[4].Text = "Reference";
            e.Row.Cells[5].Text = "Receipt";
            e.Row.Cells[6].Text = "Amount";
            e.Row.Cells[7].Text = "Date";
            e.Row.Cells[8].Text = "Date";
            e.Row.Cells[9].Text = "Date Processed";
            e.Row.Cells[10].Style.Add("display", "none");//statusname
           * */
        }

    }

    

    private int comprobarsum()
    {
        int result = 0;
        // en cualquier caso que pinte color sumatoria
        if (!tb_total.Text.Equals("") )
        {
            double temp = double.Parse(tb_total.Text);
            double ammoun = 0;
            if (temp < ammoun)
            {
                tb_total.ForeColor = System.Drawing.Color.OrangeRed;
                //tb_total.BackColor = System.Drawing.Color.OrangeRed;
                result = 0;
                lb_msg.Text = "The sum of the checks is less than the amount of deposit";
            }
            else if (temp > ammoun)
            {
                tb_total.ForeColor = System.Drawing.Color.Red;
                //tb_total.BackColor = System.Drawing.Color.Blue;
                result = 2;
                lb_msg.Text = "The sum of the checks is greater than the amount of deposit";
            }
            else
            {
                tb_total.ForeColor = System.Drawing.Color.DarkGreen;
                //tb_total.BackColor = System.Drawing.Color.Red;
                result = 1;
                lb_msg.Text = "Exit?";
            }


        }
        return result;
    }
  
}